//
//  LZTEmoticonPageViewCell.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/18.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

import UIKit

class LZTEmoticonPageViewCell: UICollectionViewCell {
    
    //定义一个一位数组 用于展示表情
    var emoticons: [LZTEmoticonModel]? {
        
        didSet {
            
            //先全部隐藏 由于 图片表情和emoji表情分别设置的title和image，加上cell复用问题，因此，需要利用一个button打开一个button来解决
            for button in buttonArray {
                button.hidden = true
            }
            
            //遍历传入的图片 根据图片个数进行button的image赋值
            for (i,emoticonModel) in emoticons!.enumerate() {
                
                //获取button
                let button = buttonArray[i]
                
                //给button的模型属性 赋值
                button.emoticonModel = emoticonModel
                
                //创建一个button 就让其显示
                button.hidden = false
                
                //判断传入的数组中 是图片表情 还是 emoji表情
                if emoticonModel.isEmoji {
                    
                    //利用第三方对象方法进行code赋值
                    let code = (emoticonModel.code ?? "" as NSString).emoji()
                    
                    //是emoji表情  code过来的值是16进制字符串
                    button.setTitle(code, forState: UIControlState.Normal)
                    
                    button.setImage(nil, forState: UIControlState.Normal)

                }else {
                    //是图片表情
//                    let png = emoticonModel.png ?? ""
//                    let path = emoticonModel.path ?? ""
//                    let image = UIImage(named: "\(path)\(png)", inBundle: LZTEmoticonTools.sharedTools.emoticonsBundle, compatibleWithTraitCollection: nil)
//                    let image = UIImage.emoticonImage(emoticonModel)
                    
                    button.setImage(UIImage.emoticonImage(emoticonModel), forState: UIControlState.Normal)
                    
                    button.setTitle(nil, forState: UIControlState.Normal)

                }
            }
        }
    }
    
    //
    var indexPath: NSIndexPath? {
        didSet{
            
            //设置text
            //messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
            
        }
    }
    
    //可遍数组--保存创建的button
    var buttonArray: [LZTEmoticonButton] = [LZTEmoticonButton]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //删除按钮的点击事件
    func deleteBtnClick() {
        
        //发送通知：告诉textView进行内容删除
        NSNotificationCenter.defaultCenter().postNotificationName(WBEMOTICONDELETEBTNNOTIFICATION, object: nil)
        
    }
    
    //20个图片表情对应的点击事件
    func buttonClick(button: LZTEmoticonButton) {
        
        //发送通知 告知textview下方 表情按钮被点击
        NSNotificationCenter.defaultCenter().postNotificationName(WBEMOTICONBTNNOTIFICATION, object:button.emoticonModel)
        
    }
    
    //设置视图
    func setupUI() {
        
        //backgroundColor = randomColor()
        //添加控件
        addChildButtons()
        //添加删除按钮
        contentView.addSubview(deleteBtn)
        //contentView.addSubview(messageLabel)
        //添加约束
//        messageLabel.snp_makeConstraints { (make) in
//            make.center.equalTo(contentView)
//        }
        
    }
    
    //布局buttons
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //button的  宽  高
        let w = (self.frame.width - 10) / CGFloat(LZTEmoticonMaxCol)
        let h = (self.frame.height - 20) / CGFloat(LZTEmoticonMaxRow)
        
        //遍历button
        for (index, button) in buttonArray.enumerate() {
            
            //列数
            let col = CGFloat(index % LZTEmoticonMaxCol)
            //行数
            let row = CGFloat(index / LZTEmoticonMaxCol)
            //设置frame
            button.frame = CGRect(x: col * w + 5, y: row * h, width: w, height: h)
            
        }
        //给删除按钮设置frame
        deleteBtn.frame = CGRect(x: self.frame.width - 5 - w, y: h * 2, width: w, height: h)
        
    }
    
    //每一个cell上创建20个按钮
    func addChildButtons() {
        //循环创建button
        for _ in 0..<LZTEmoticonMaxCount {
            
            let button = LZTEmoticonButton()
            
            //添加点击事件
            button.addTarget(self, action: #selector(LZTEmoticonPageViewCell.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            
            // 设置字体大小 其实就是让 表情变大
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
            
            //button.backgroundColor = randomColor()
            
            buttonArray.append(button)
            
            contentView.addSubview(button)
        }
    }
    
    //懒加载控件 用于看清当前cell是 在哪个组的第几个
    private lazy var messageLabel: UILabel = UILabel(textColor: UIColor.redColor(), textSize: 35 , maxWidth: SCREENW)
    
    //懒加载 删除按钮
    private lazy var deleteBtn: UIButton = {
        
        let btn = UIButton()
        //点击事件
        btn.addTarget(self, action: #selector(LZTEmoticonPageViewCell.deleteBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return btn
    }()
    
    
}
